package com.edu.mail.product.controller;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import com.edu.mail.product.bean.Product;
import com.edu.mail.product.mapper.ProductMapper;
import com.edu.mail.product.web.Response;

@RestController
public class ProductController {

	@Autowired
	private ProductMapper productMapper;
	
	@PostMapping("/soa/product/add")
	public Object add(Product product){
		Integer res = productMapper.add(product);
		return res==1 ? new Response("200","ok") : new Response("500","fail");
	}
	
	@GetMapping("/soa/product/{id}")
	public Object get(@PathVariable("id") Integer id){
		Product product = productMapper.getById(id);
		Queue q = new LinkedBlockingQueue();
		return new Response("200","ok",product);
	}
	
	@GetMapping("/soa/products")
	public Object list(){
		List<Product> list = productMapper.queryByLists();
		return new Response("200","ok",list);
	}
	
	//http://localhost:8080/soa/product/update?pname=iphone&type=电子产品&price=555&pid=1 路径传参
	@PutMapping("/soa/product/update")
	public Object update(Product product){
		//System.out.println(product);
		Integer res = productMapper.update(product);
		return res==1 ? new Response("200","ok") : new Response("500","fail");
	}
	
	@DeleteMapping("/soa/product/delete/{id}")
	public Object delete(@PathVariable("id") Integer id){
		Integer res = productMapper.deleteById(id);
		return res==1 ? new Response("200","ok") : new Response("500","fail");
	}
	
}
